package com.scuvc.controller.user;

import com.scuvc.entity.RecordInfo;
import com.scuvc.utils.ImageUtils;
import com.scuvc.utils.JMenuBarUtils;
import com.scuvc.utils.StaticUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

public class QueryRecordView extends JFrame {

    public void queryStart(){
        setTitle("城市租车系统");
        //设置窗体起始位置
        setLocation(100,50);
        //设置窗体大小
        setSize(1170,670);
        //把图片添加到标签里
        ImageIcon img = new ImageIcon("src/images/bg_admin.jpg");
        ImageIcon bg = ImageUtils.createAlphaIcon(img,0.5f,
                img.getIconWidth(),img.getIconHeight());
        JLabel label = new JLabel(bg);
        //设置标签的大小
        label.setSize(bg.getIconWidth(),bg.getIconHeight());
        //把标签放在分层面板的最底层
        getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
        //把窗口面板设为内容面板并设为透明，流动布局
        JPanel pan = (JPanel)getContentPane();
        pan.setOpaque(false);
        pan.setLayout(new BorderLayout());
        JPanel panCenter = new JPanel();
        FlowLayout fl = new FlowLayout();
        fl.setHgap(0);
        //设置标题栏
        setTop(pan,panCenter);
        //设置左边菜单栏
        setLeft(pan);
        //设置中间表格部分
        panCenter.setLayout(fl);
        panCenter.setOpaque(false);
        //边框
        panCenter.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        panCenter.setOpaque(false);
        add(panCenter,BorderLayout.CENTER);
        //设置中间表格表头部分
        setCenterTitle(panCenter);
        //设置中间表格数据部分
        StaticUtils.refreshC = setCenterData(panCenter);

        //分页
        JPanel panDown = new JPanel();
        panDown.setLayout(new FlowLayout());
        panDown.setOpaque(false);

        JButton firstPage = new JButton("首\u3000页");
        JButton endPage = new JButton("尾\u3000页");
        JButton prePage = new JButton("上一页");
        JButton nextPage = new JButton("下一页");
        add(panDown,BorderLayout.SOUTH);

        java.util.List<JButton> jbList = new ArrayList<>();
        //存储JButton按钮的集合
        for(int i=0;i<StaticUtils.pageBeanR.getTotalPage();i++){
            jbList.add(new JButton(i+1+""));
        }
        ActionListener actionListener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //清理对应原始页码的数据
                for(int i=0;i<StaticUtils.refreshC.size();i++){
                    panCenter.remove(StaticUtils.refreshC.get(i));
                }
                JButton jButton = (JButton) e.getSource();
                int page = Integer.valueOf(jButton.getText());
                StaticUtils.pageBeanR.setCurPage(page);
                StaticUtils.pageBeanR.setPrePage();
                StaticUtils.pageBeanR.setNextPage();
                StaticUtils.pageBeanR.setDataList(StaticUtils.recordInfoService.
                        queryRecordInfoByUserId(StaticUtils.user.getId()));
                StaticUtils.pageBeanR.setTotalPage();
                //设置数据
                List<JComponent> jc = setCenterData(panCenter);
                //刷新数据
                StaticUtils.refreshC = jc;
                //刷新界面
                repaint();
            }
        };
        panDown.add(firstPage);
        panDown.add(prePage);
        for(int i=0;i<jbList.size();i++){
            panDown.add(jbList.get(i));
            jbList.get(i).addActionListener(actionListener);
        }
        panDown.add(nextPage);
        panDown.add(endPage);
        ActionListener actionListener1 = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //清理对应原始页码的数据
                for(int i=0;i<StaticUtils.refreshC.size();i++){
                    panCenter.remove(StaticUtils.refreshC.get(i));
                }
                JButton jButton = (JButton) e.getSource();
                String text = jButton.getText();
                int index = 1 ;
                if(text.equals("尾\u3000页")){
                    index = StaticUtils.pageBeanR.getTotalPage();
                }else if(text.equals("上一页")){
                    index = StaticUtils.pageBeanR.getCurPage()-1;
                }else if(text.equals("下一页")){
                    index = StaticUtils.pageBeanR.getCurPage()+1;
                }
                StaticUtils.pageBeanR.setCurPage(index);
                StaticUtils.pageBeanR.setPrePage();
                StaticUtils.pageBeanR.setNextPage();
                StaticUtils.pageBeanR.setDataList(StaticUtils.recordInfoService.
                        queryRecordInfoByUserId(StaticUtils.user.getId()));
                StaticUtils.pageBeanR.setTotalPage();
                //设置数据
                List<JComponent> jc = setCenterData(panCenter);
                //刷新数据
                StaticUtils.refreshC = jc;
                //刷新界面
                repaint();
            }
        };
        firstPage.addActionListener(actionListener1);
        endPage.addActionListener(actionListener1);
        prePage.addActionListener(actionListener1);
        nextPage.addActionListener(actionListener1);
        //设置窗体大小是否可调整
        setResizable(false);
        //设置窗体是否可见
        setVisible(true);
        //单击关闭按钮执行的操作
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    private java.util.List<JComponent> setCenterData(JPanel panCenter) {
        java.util.List<RecordInfo> recordList = StaticUtils.recordInfoService.
                queryRecordInfoByUserIdPage(StaticUtils.user.getId(),StaticUtils.pageBeanR.getCurPage(),
                        StaticUtils.pageBeanR.getPageSize());
        java.util.List<JComponent> list = new ArrayList<>();
        for(int i=0;i<recordList.size();i++){
            list.add(new JLabel(recordList.get(i).getId()+""));
            list.add(new JLabel(recordList.get(i).getCarInfo().getCarNo()+""));
            list.add(new JLabel(recordList.get(i).getCarInfo().getBrand().getBrand_name()));
            list.add(new JLabel(recordList.get(i).getCarInfo().getModel().getModel_name()));
            list.add(new JLabel(recordList.get(i).getCarInfo().getCategory().getCategory_name()));
            list.add(new JLabel(recordList.get(i).getRent_date()==null?"yyyy/MM/dd":
                    StaticUtils.format(recordList.get(i).getRent_date())));
            list.add(new JLabel(recordList.get(i).getReturn_date()==null?"yyyy/MM/dd":
                    StaticUtils.format(recordList.get(i).getReturn_date())));
            list.add(new JLabel(recordList.get(i).getPrice()+""));
            list.add(new JLabel(recordList.get(i).getRent_amount()+""));
        }
        //保存数据
        StaticUtils.refreshC = list ;
        Font font = new Font("仿宋",Font.PLAIN,18);
        for(int i=0;i<list.size();i++){
            list.get(i).setFont(font);
            panCenter.add(list.get(i));
            //设置标签的边框线
            list.get(i).setBorder(BorderFactory.createLineBorder(Color.BLACK));
            //设置标签的列宽
            list.get(i).setPreferredSize(new Dimension(120,40));
            if(list.get(i) instanceof JLabel){
                ((JLabel) list.get(i)).setHorizontalAlignment(SwingConstants.CENTER);
            }
        }
        return list ;
    }

    private java.util.List<JComponent> setCenterDataCarNo(JPanel panCenter,String carNo) {
        java.util.List<RecordInfo> recordList = StaticUtils.recordInfoService.
                queryRecordInfoByUserIdByCarNo(StaticUtils.user.getId(),carNo,
                        StaticUtils.pageBeanR.getCurPage(),
                        StaticUtils.pageBeanR.getPageSize());
        java.util.List<JComponent> list = new ArrayList<>();
        for(int i=0;i<recordList.size();i++){
            list.add(new JLabel(recordList.get(i).getId()+""));
            list.add(new JLabel(recordList.get(i).getCarInfo().getCarNo()+""));
            list.add(new JLabel(recordList.get(i).getCarInfo().getBrand().getBrand_name()));
            list.add(new JLabel(recordList.get(i).getCarInfo().getModel().getModel_name()));
            list.add(new JLabel(recordList.get(i).getCarInfo().getCategory().getCategory_name()));
            list.add(new JLabel(recordList.get(i).getRent_date()==null?"yyyy/MM/dd":
                    StaticUtils.format(recordList.get(i).getRent_date())));
            list.add(new JLabel(recordList.get(i).getReturn_date()==null?"yyyy/MM/dd":
                    StaticUtils.format(recordList.get(i).getReturn_date())));
            list.add(new JLabel(recordList.get(i).getPrice()+""));
            list.add(new JLabel(recordList.get(i).getRent_amount()+""));
        }
        //保存数据
        StaticUtils.refreshC = list ;
        Font font = new Font("仿宋",Font.PLAIN,18);
        for(int i=0;i<list.size();i++){
            list.get(i).setFont(font);
            panCenter.add(list.get(i));
            //设置标签的边框线
            list.get(i).setBorder(BorderFactory.createLineBorder(Color.BLACK));
            //设置标签的列宽
            list.get(i).setPreferredSize(new Dimension(120,40));
            if(list.get(i) instanceof JLabel){
                ((JLabel) list.get(i)).setHorizontalAlignment(SwingConstants.CENTER);
            }
        }
        return list ;
    }

    private void setCenterTitle(JPanel panCenter) {
        panCenter.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        panCenter.setPreferredSize(new Dimension(panCenter.getWidth(),60));
        java.util.List<String> jLabelName = new ArrayList<>();
        jLabelName.add("租赁编号");
        jLabelName.add("车\u0020牌\u0020号");
        jLabelName.add("品\u3000\u3000牌");
        jLabelName.add("模\u3000\u3000式");
        jLabelName.add("类\u3000\u3000别");
        jLabelName.add("租车时间");
        jLabelName.add("还车时间");
        jLabelName.add("每日租金");
        jLabelName.add("租金总额");
        List<JLabel> list = new ArrayList<>();
        for(int i=0;i<jLabelName.size();i++){
            list.add(new JLabel(jLabelName.get(i)));
        }
        Font font = new Font("仿宋",Font.BOLD,24);
        for(int i=0;i<list.size();i++){
            list.get(i).setFont(font);
            panCenter.add(list.get(i));
            //设置标签的边框线
            list.get(i).setBorder(BorderFactory.createLineBorder(Color.BLACK));
            //设置标签的列宽
            list.get(i).setPreferredSize(new Dimension(120,60));
            list.get(i).setHorizontalAlignment(SwingConstants.CENTER);
        }
    }

    private void setLeft(JPanel pan) {
        JPanel panLeft = new JPanel();
        panLeft.setLayout(new GridLayout(0,1));
        panLeft.setOpaque(false);
        //将左边面板添加到面板的左边
        pan.add(panLeft,BorderLayout.WEST);
        java.util.List<String> jMenuName = new ArrayList<>();
        jMenuName.add("业务办理");
        jMenuName.add("记录查询");
        jMenuName.add("个人中心");
        String[][] itemName = {
                {"租赁汽车","归还汽车"},
                {"查询记录"},
                {"个人信息","修改密码"}};
        JMenuBar jMenuBar = JMenuBarUtils.createJMenuBar
                (jMenuName,65,0, itemName);
        panLeft.add(jMenuBar);
        add(panLeft,BorderLayout.WEST);

        ActionListener carManage = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JMenuItem jmi = (JMenuItem) e.getSource();
                String text = jmi.getText();
                if(text.equals("租赁汽车")){
                    RentCarView rcv = new RentCarView();
                    setVisible(false);
                    rcv.rentStart();
                }else if(text.equals("归还汽车")){
                    ReturnCarView rcv = new ReturnCarView();
                    setVisible(false);
                    rcv.returnStart();
                }
            }
        };
        StaticUtils.jmiList[0][0].addActionListener(carManage);
        StaticUtils.jmiList[0][1].addActionListener(carManage);
    }
    //设置顶部
    private void setTop(JPanel pan,JPanel panCenter) {
        JPanel panTop = new JPanel();
        GridLayout grid = new GridLayout(2,1);
        grid.setVgap(30);
        panTop.setLayout(grid);
        panTop.setOpaque(false);
        JLabel jTop = new JLabel("智能管家");
        jTop.setPreferredSize(new Dimension(jTop.getPreferredSize().width,40));
        jTop.setFont(new Font("仿宋",Font.BOLD,36));
        jTop.setHorizontalAlignment(SwingConstants.CENTER);

        JPanel panTop2 = new JPanel();

        GridLayout gril1 = new GridLayout(1,8);
        gril1.setHgap(10);
        panTop2.setLayout(gril1);
        panTop2.setOpaque(false);
        panTop2.setPreferredSize(new Dimension(panTop2.getPreferredSize().width,40));
        Font font = new Font("仿宋",Font.BOLD,20);
        JLabel j2L1 = new JLabel();
        JLabel j2L2 = new JLabel();
        JLabel j2L3 = new JLabel();
        JLabel j2L4 = new JLabel();
        JLabel j2L5 = new JLabel();

        JTextField j2f6 = new JTextField();
        j2f6.setFont(font);
        String[] cItems = {"车牌号查询"};
        JComboBox<String> comboBoxR = new JComboBox<>(cItems);
        comboBoxR.setFont(font);
        JButton jButton = new JButton("搜\u3000索");

        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                for(int i=0;i<StaticUtils.refreshC.size();i++){
                    panCenter.remove(StaticUtils.refreshC.get(i));
                }
                String choose = (String) comboBoxR.getSelectedItem();
                if(choose.equals("车牌号查询")){
                    String carNo = j2f6.getText();
                    StaticUtils.pageBeanR.setCurPage(1);
                    StaticUtils.pageBeanR.setPrePage();
                    StaticUtils.pageBeanR.setNextPage();
                    StaticUtils.pageBeanR.setDataList(StaticUtils.recordInfoService.
                            queryRecordInfoByUserIdByCarNo(StaticUtils.user.getId(),carNo));
                    StaticUtils.pageBeanR.setTotalPage();
                    //设置数据
                    List<JComponent> jc = setCenterDataCarNo(panCenter,carNo);
                    //刷新数据
                    StaticUtils.refreshC = jc;
                    //刷新界面
                    repaint();
                }
            }
        });

        panTop2.add(j2L1);
        panTop2.add(j2L2);
        panTop2.add(j2L3);
        panTop2.add(j2L4);
        panTop2.add(j2L5);
        panTop2.add(j2f6);
        panTop2.add(comboBoxR);
        panTop2.add(jButton);
        panTop.add(jTop);
        panTop.add(panTop2);
        pan.add(panTop,BorderLayout.NORTH);
    }
}
